const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const koaStatic = require('koa-static')
const path = require('path')
const cors = require('./middleware/cors')
const upload = require('./middleware/upload')
const router = require('./routes/router')
const { REDIS_CONF, SERVICE_PORT } = require('./config/setting')
const session = require('koa-generic-session')
const redisStore = require('koa-redis')


const app = new Koa();

app.keys = ['key', 'keyskeys'];
// 对session进行加密的key
app.use(session({
  prefix: "prefix", // 给session对象在redis存储的地址名前面添加的前缀内容
  key: "webapp", // key名
  store: redisStore({
    all: REDIS_CONF.host + ':' + REDIS_CONF.port
  })
}))

app.use(koaStatic(path.join(__dirname, './static')))
app.use(upload)
app.use(bodyParser());
app.use(cors)

router(app);

app.listen(SERVICE_PORT, () => console.log(`http://localhost:${SERVICE_PORT}`));

app.on('error', (err, ctx) => {
  console.error('server error', err, ctx)
});
